// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property(cc.Node)
    btnClearAll:cc.Node=null;

    @property(cc.Prefab)
    label:cc.Prefab;

    @property(cc.ScrollView)
    scrollView:cc.ScrollView;

    _assets=[];

    _hasLoading=false;
    // LIFE-CYCLE CALLBACKS:

    onLoad () {

        this.init();
    }
    //初始化
    private init(){

        this._assets=[];

        this._hasLoading=false;

        this.scrollView.content.height=0;

        this.btnClearAll.active=false;
    }
    
    //给i18n标签赋值
    private createLabel(text:string){

        let node=cc.instantiate(this.label) as cc.Node;

        let label=node.getComponent(cc.Label);

        label.textKey=text;

        this.scrollView.content.addChild(node);
    }

    private clear(){

        this.scrollView.content.removeAllChildren(true);

        for(let i=0;i<this._assets.length;++i){

            let asset=this._assets[i];

            cc.loader.release(asset);
        }
    }

    private onClearAll(){

        this.scrollView.content.height=0;

        this.btnClearAll.active=false;

        this.clear();
    }

    private onLoadAll(){

        if(this._hasLoading==true){return;}

        this._hasLoading=true;

        this.clear();

        this.createLabel("Load All Assets");

        this.scrollView.scrollToTop();

        //载入资源，以及回调函数处理错误
        cc.loader.loadResDir("test assets",(err,assets)=>{

            //如果该对象不可用，即销毁了，则返回
            if(this.isValid==false){return;}

            this._assets=assets;

            for(let i=0;i<assets.length;++i){

                let asset=assets[i];

                let info=asset.toString();

                //如果信息为空
                if(info===null){
                    //如果是json格式的文件
                    if(asset instanceof cc.JsonAsset){
                        
                        info=JSON.stringify(asset.json,null,4);
                    }
                    else {
                        info=info|| asset.name||cc.js.getClassName(asset);
                    }
                }

                this.createLabel(info);
            }

            this._hasLoading=false;

            this.btnClearAll.active=true;
        })

    }

    private onLoadSprtieFrameAll(){

        //已经载入了，则返回
        if(this._hasLoading===true){return;}

        this._hasLoading=true;

        this.clear();

        this.createLabel("Load All Sprite Frame");

        this.scrollView.scrollToTop();

         //载入资源目录，以及回调函数处理错误
        cc.loader.loadResDir("test assets",cc.SpriteFrame,(err,assets)=>{

            //不存在，则返回
            if(this.isValid===false){
                return;
            }

            this._assets=assets;

            for(let i=0;i<assets.length;++i){

                let asset=assets[i];

                this.createLabel(asset.name);
            }

            this._hasLoading=false;

            this.btnClearAll.active=true;
        })
    }
    // update (dt) {}
}
